package mygang.helper;

import java.util.ArrayList;
import java.util.Formatter;
import javax.servlet.http.HttpServletRequest;
import mygang.model.BaseModel;

/**
 * 
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 */
public class ContentHelper extends BaseModel {
    
    private HttpServletRequest request = null;
    
    //content folders
    private static final String CONTENT     = "content";
    private static final String CSS         = "css";
    private static final String JAVASCRIPT  = "js";
    private static final String IMAGES      = "img";

    //templates
    private static final String TEMPLATE_CSS        = "<link href='%s' rel='stylesheet' type='text/css'/>";
    private static final String TEMPLATE_JAVASCRIPT = "<script src='%s' language='javascript' type='text/javascript'></script>";
    
    private static final ArrayList<String> IMAGE_EXTENSIONS = new ArrayList<String>(){{
        add("png");
        add("jpg");
        add("jpeg");
        add("gif");
        add("bmp");
    }};
    
    // -------------------------------------------------------------------------
    
    /**
     * Constructs an instance
     * @param request defines request
     */
    public ContentHelper(HttpServletRequest request){
        this.request = request;
    }
    
    // -------------------------------------------------------------------- Root
    
    private String getContentRoot(){
        return formatPath(getURL(), CONTENT);
    }

    private String getImageRoot(){
        return formatPath(getContentRoot(), IMAGES);
    }
    
    private String getCssRoot(){
        return formatPath(getContentRoot(), CSS);
    }

    private String getJsRoot(){
        return formatPath(getContentRoot(), JAVASCRIPT);
    }
    
    // --------------------------------------------------------------------- URL

    public String getImageUrl(String filename){
        return formatPath(getImageRoot(), filename);
    }
   
    private String getCssUrl(String filename){
        return formatPath(getCssRoot(), filename);
    }

    private String getJsUrl(String filename){
        return formatPath(getJsRoot(), filename);
    }
    
    // -------------------------------------------------------------------------
    
    private String format(String format, String arg1, String arg2){
        Formatter formatter = new Formatter();
        formatter.format(format, arg1, arg2);
        return formatter.toString();
    }
    
    private String format(String format, String arg1){
        Formatter formatter = new Formatter();
        formatter.format(format, arg1);
        return formatter.toString();
    }
    
    private String formatPath(String arg1, String arg2){
        if(arg2.isEmpty() && arg1.isEmpty()) return "";
        if(arg1.isEmpty()) return arg2;
        if(arg2.isEmpty()) return arg1;
        
        return format("%s/%s", arg1, arg2);
    }
    
    // -------------------------------------------------------------------------
    
    /**
     * Returns page url
     * @return page url
     */
    public String getURL(){
        String protocol = request.getScheme();
        String server = request.getServerName();
        int port = request.getServerPort();
        String context = request.getContextPath();
        
        return protocol + "://" + server + (port == 80 ? "" : ":"+port) + context;
    }
    
    /**
     * Returns prepared url (concatenates site url with specified part of url)
     * @param urlPart defines specified part of url
     * @return prepared url
     */
    public String prepareURL(String urlPart){
        return formatPath(getURL(), urlPart);
    }
    
    /**
     * Returns script tag
     * @param filename defines javascript file
     * @return script tag
     */
    public String getJs(String filename){
        return format(TEMPLATE_JAVASCRIPT, getJsUrl(filename));
    }

    /**
     * Returns link tag
     * @param filename defines css file
     * @return link tag
     */
    public String getCss(String filename){
        return format(TEMPLATE_CSS, getCssUrl(filename));
    }
    
    public String getFile(String filename){
        String extension = getFileExtension(filename);
        if(extension.equals("js")){
            return getJs(filename);
        }
        if(extension.equals("css")){
            return getCss(filename);
        }
        return "";
    }
    
    public String getFileUrl(String filename){
        String extension = getFileExtension(filename);
        if(extension.equals("js")){
            return getJsUrl(filename);
        }
        if(extension.equals("css")){
            return getCssUrl(filename);
        }
        if(IMAGE_EXTENSIONS.indexOf(extension)>-1){
            return getImageUrl(filename);
        }
        return "";
    }
    
    private static String getFileExtension(String filename){
        String extension = "";
        int dotIndex = filename.lastIndexOf(".");
        if(dotIndex>-1 && (dotIndex+1)<filename.length()){
            extension = filename.substring(dotIndex+1).toLowerCase();
        }
        return extension;
    }
}